package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.EngineerProjectResult;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mlli on 2017/11/3.
 * 选择工程动态项目
 */

public class EngineerProjectAdapter extends RecyclerView.Adapter {

    private ResultListener resultListener;

    public void setResultListener(ResultListener resultListener) {
        this.resultListener = resultListener;
    }

    public interface ResultListener {
        void setMsg(String msg, String id);
    }

    private Context context;
    List<EngineerProjectResult.EngProject> engineer;

    public EngineerProjectAdapter(Context context, List<EngineerProjectResult.EngProject> engineer) {
        this.context = context;
        this.engineer = engineer;
    }

    public void setEngineer(List<EngineerProjectResult.EngProject> engineer) {
        this.engineer = engineer;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.left_textview, null);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        EngineerProjectResult.EngProject project = engineer.get(position);
        MHolder mHolder = (MHolder) holder;
        mHolder.textView.setText(project.getProJectName());
        EngineerProjectResult.EngProject bean = engineer.get(position);
        final String proJectName = bean.getProJectName();
        final int autoId = bean.getAutoId();
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (resultListener != null) resultListener.setMsg(proJectName, autoId + "");
            }
        });
    }

    @Override
    public int getItemCount() {
        return engineer == null ? 0 : engineer.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView textView;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
